<?php $this->html->loadCSS("Content/gmode_newproduct/css/tables.css")?>
<?php $this->html->loadJS("Content/gmode_newproduct/js/jquery.paginatetable.js")?>
<script type="text/javascript">
	$(document).ready(function(){
		//Table pagging - sort
		$('#TablePaginate').paginateTable({ rowsPerPage: 2 });
		$('#TablePaginate').tablesort();
	});
</script>

<div class="column_center_bg1">
<div class="column_center_bg2">
<table style="width: 100%; border: 1px solid #c0c0c0;">
	<tbody>
		<tr>
			<td style="width: 100%;">
			<div class="product_box3">
			<div class="row1">
			<div class="bg_inner1">
			<div class="bg_inner2">
			<div class="bg_inner3"><img src="images/spacer.gif" alt="" width="1"
				height="1"><br>
			</div>
			</div>
			</div>
			</div>
			<div class="clear"></div>
			<div class="row2">
			<div class="bg_inner1">
			<div class="bg_inner2">
			<div class="bg_inner311" style="padding: 2px;">
			<div class="column-center-background"><!--content_center--> <!-- bof breadcrumb -->
			<!-- eof breadcrumb --> <!-- bof upload alerts --> <!-- eof upload alerts -->

			<div class="centerColumn" id="contactUsDefault">
			<address><strong>
				<?php echo $this->settings['company_name']?><br>
				Điện thoại liên hệ: <?php echo $this->settings['phone']?><br>
				Fax: <?php echo $this->settings['fax']?><br>
				Địa chỉ: <?php echo $this->settings['address']?><br>
				</strong>
			</address>
			<div id="contactUsNoticeContent" class="content">
			<p><strong>Giỏ hàng hiện tại:</strong></p>
			</div>
			<?php echo $this->html->beginForm("create");?>
				<table id="TablePaginate" class="datatable tablesort selectable paginate full">
					<thead>
					<tr>
						<th style="width: 40px;">STT</th>
						<th style="width: 80px;">Mã sản phẩm</th>
						<th style="width: 180px;">Sản phẩm</th>
						<th style="width: 70px;">Số lượng</th>
						<th style="width: 90px;">Màu sắc</th>
						<th style="width: 90px;">Kích cỡ</th>
						<th style="width: 80px;">Giá</th>
						<th style="width: 80px;">Thành tiền</th>
						<th>Lựa chọn</th>
					</tr>
					</thead>
					<tbody>
					<?php $i = 0;?>
					<?php foreach ($this->productList as $product)
					{
						$i++;
					?>
					<tr class="<?php echo $product["id"]?>">
						<td class="<?php echo $product["id"]?>"><?php echo $i?></td>
						<td class="<?php echo $product["id"]?>"><?php echo $product["id"]?></td>
						<td class="<?php echo $product["id"]?>"><?php echo $product["name"]?></td>
						<td class="<?php echo $product["id"]?>">
							<input type="text" size="1" class="quantity"  ajaxify="<?php echo $this->url->action("updateCart", "ShoppingCart")?>" data="<?php echo $product["id"]?>" value="<?php echo $product["quantity"]?>" />
						</td>
						<td class="<?php echo $product["id"]?>"><?php echo $product['color']?></td>
						<td class="<?php echo $product["id"]?>">
							<?php
								$arr = explode(',', $product['size']);
								foreach ($arr as $item)
								{
									echo $this->modelSizes->getName($item).",";
								}
							?>
						</td>
						<td class="<?php echo $product["id"]?>"><?php echo number_format($product["price"], 0, ',', '.')?></td>
						<td rel="sum" class="<?php echo $product["id"]?>"><?php echo number_format((int)$product["quantity"] * (float)$product["price"], 0, ',', '.')?></td>
						<td class="<?php echo $product["id"]?>">
							<a onclick="deleteCart(this);" ajaxify="<?php echo $this->url->action("deleteCart", "ShoppingCart")?>" data="<?php echo $product["id"]?>">Xóa</a>
						</td>
					</tr>
					<?php
					}?>
					</tbody>
					<tfoot>
						<tr>
							<th colspan="6" align="left">
								<input type="button" value="Cập nhật" class="button-gray" onclick="updateCart();" />
								<img src='<?php echo $this->url->content("Content/gmode_newproduct/images/loading.gif")?>' id="update_loading" style="display:none;" />
							</th>
							<th class="totalOrder" align="right" colspan="3">
								<b>Tổng tiền : </b><span><?php echo $this->total?></span>
							</th>
						</tr>
					</tfoot>
				</table>
			<?php echo $this->html->endForm();?>
			</div>
			<div id='pager' class='pager'>
				<li><a href='#' alt='Previous' class='prevPage button-gray'>Trước</a></li>
				<span class='listPages'></span>
				<li><a href='#' alt='Next' class='nextPage button-gray'>Kế tiếp</a></li>
				<span class="button-gray" style='float:right; height: 20px; padding: 0 5px 0 5px;'>Đang xem <span class='currentPage'></span> của <span class='totalPages'></span></span>
			</div>
			<div style="clear: both"></div>
			<div>
				<div style="text-align: left; margin-top: 10px; float: left">
					<input class="button-gray" type="button" onclick="window.location.href = '<?php echo $this->url->action("index", "Products")?>';" value="Tiếp tục mua hàng">
				</div>
				<div style="text-align: right; margin-top: 10px; float: right">
					<input class="button-gray" type="button" onclick="window.location.href = '<?php echo $this->url->action("checkout")?>';" value="Thanh toán">
				</div>
				<div style="clear: both;"></div>
			</div>
			<div style="clear: both"></div>
			<!--eof content_center--></div>
			<div class="clear"></div>
			</div>
			</div>
			</div>
			</div>
			<div class="row3">
			<div class="bg_inner1">
			<div class="bg_inner2">
			<div class="bg_inner3"><img src="images/spacer.gif" alt="" width="1"
				height="1"></div>
			</div>
			</div>
			</div>
			</div>
			</td>
		</tr>
	</tbody>
</table>
</div>
</div>
<div class="column_center_bg3">
<div class="column_center_bg4">
<div class="column_center_bg5"><img src="images/spacer.gif" alt=""
	width="1" height="1"></div>
</div>
</div>
<script type="text/javascript">
function deleteCart(obj)
{
	$("#update_loading").show();
	
	$.post($(obj).attr("ajaxify"), {id: $(obj).attr("data")}, function(res){
			if(res.success)
			{
				$("tr." + $(obj).attr("data")).remove();
				//Cập nhật lại tổng tiền
				$(".totalOrder span").html(res.total);
				//Cập nhật lại tổng số sản phẩm
				$("#_cartItemCount").html(res.cart_total);
			}
			//Ẩn loading
			$("#update_loading").hide();
		}, "json");
}
function updateCart()
{
	$("#update_loading").show();
	
	$.each($(".quantity"), function(k, v){
		$.post($(v).attr("ajaxify"), {id: $(v).attr("data"), quantity: $(v).val()}, function(res){
			if(res.success)
			{
				$("td." + res.id + "[rel='sum']").html(res.sum);
				//Cập nhật lại tổng tiền
				$(".totalOrder span").html(res.total);
				//Cập nhật lại tổng số sản phẩm
				$("#_cartItemCount").html(res.cart_total);
			}

			//Ẩn loading
			$("#update_loading").hide();
			
		}, "json");	
	});
}
</script>